#include <Riostream.h>
#include <CpLog.h>
#include <CpMagneticFieldMap.h>
#include <TSystem.h>
#include <CpPolynom1D.h>
#include <CpPolynom2D.h>
#include <CpPolynom3D.h>
#include <TF1.h>
#include <TF3.h>
#include <TStopwatch.h>
#include <CpFitter.h>
#include <CpMain.h>
int main(int argc, const char* argv[])
{
  TStopwatch timer;
  timer.Start();
  Int_t debugLevel=0;
  TString cfgFileName("CpConfig.cfg");
  TString outputDir("output");
  TString tmp;
  for (Int_t i=0;i<argc;i++) {
    tmp = argv[i];
    if (!tmp.CompareTo("--debug")) {
      tmp = argv[i+1];
      debugLevel = tmp.Atoi();
      CpLog::SetClassDebugLevel("CpPolynom1D", CpLog::kDebug+debugLevel);
      CpLog::SetClassDebugLevel("CpPolynom2D", CpLog::kDebug+debugLevel);
      CpLog::SetClassDebugLevel("CpPolynom3D", CpLog::kDebug+debugLevel);
      CpLog::SetClassDebugLevel("CpMagneticFieldMap", CpLog::kDebug+debugLevel);
      CpLog::SetClassDebugLevel("CpMagneticFieldMapObject", CpLog::kDebug+debugLevel);
    } else if (!tmp.CompareTo("--config")) {
      cfgFileName = argv[i+1];
    } else if (!tmp.CompareTo("--output")) {
      outputDir = argv[i+1];
    }
  }

  ifstream fileIn;
  fileIn.open(cfgFileName.Data());
  if (!fileIn.is_open()) {
    Printf("Error :: Config file '%s' was not found !!! Exiting ...",cfgFileName.Data());
    return 1;
  }

  TString fitStrings[16];
  Int_t count = 0;
  // Read the input list of files and add them to the chain
  TString fileItem;
  while (fileIn.good()) {
    fileIn >> fileItem;
    fitStrings[count++] = fileItem;
  }
  fileIn.close();

  CpMagneticFieldMap fieldMap("MyTestField","My Test Filed");
  fieldMap.ImportFromTxtFile("data/magnets/txt/Magnet_001.txt","data/magnets/root/Magnet_001.root");
  fieldMap.SetMagneticMapRootFileName("data/magnets/root/Magnet_001.root");

//   Int_t polynomNum = fitStrings[0].Atoi();
//   Printf(Form("Fitting with polynom %d.",polynomNum));
//   CpFitter fitter;
//   fitter.Fit1D(&fieldMap,polynomNum,fitStrings[1].Atof(),fitStrings[2].Atof(),fitStrings[3].Atoi());
//   fitter.Fit2D(&fieldMap,polynomNum,fitStrings[4].Atof(),fitStrings[5].Atof(),fitStrings[6].Atof(),fitStrings[7].Atof(),fitStrings[8].Atoi());
//   fitter.Fit3D(&fieldMap,polynomNum,fitStrings[9].Atof(),fitStrings[10].Atof(),fitStrings[11].Atof(),fitStrings[12].Atof(),fitStrings[13].Atof(),fitStrings[14].Atof(),fitStrings[15].Atoi());
// 
// //   gSystem->MakeDirectory(outputDir.Data());
// //   gSystem->Exec(Form("mv *.root %s/",outputDir));

  timer.Stop();
  timer.Print();
  return 0;
}



// TF1 *myPol_1D=0;
// TF2 *myPol_2D=0;
// TF3 *myPol_3D=0;

//   myPol_1D = new TF1("myPol_1D",&CpPolynom1D::DoPolynom9,-3.1,3.1,10);


// myPol_2D = new TF2("myPol_2D",&CpPolynom2D::DoPolynom0,-3.1,3.1,0.3,4.9,1);
// myPol_2D = new TF2("myPol_2D",&CpPolynom2D::DoPolynom1,-3.1,3.1,0.3,4.9,3);
// myPol_2D = new TF2("myPol_2D",&CpPolynom2D::DoPolynom2,-3.1,3.1,0.3,4.9,6);
// myPol_2D = new TF2("myPol_2D",&CpPolynom2D::DoPolynom3,-3.1,3.1,0.3,4.9,10);
// myPol_2D = new TF2("myPol_2D",&CpPolynom2D::DoPolynom4,-3.1,3.1,0.3,4.9,15);
// myPol_2D = new TF2("myPol_2D",&CpPolynom2D::DoPolynom5,-3.1,3.1,0.3,4.9,21);
// myPol_2D = new TF2("myPol_2D",&CpPolynom2D::DoPolynom6,-3.1,3.1,0.3,4.9,28);
// myPol_2D = new TF2("myPol_2D",&CpPolynom2D::DoPolynom7,-3.1,3.1,0.3,4.9,36);
// myPol_2D = new TF2("myPol_2D",&CpPolynom2D::DoPolynom8,-3.1,3.1,0.3,4.9,45);
// myPol_2D = new TF2("myPol_2D",&CpPolynom2D::DoPolynom9,-3.1,3.1,0.3,4.9,55);

// myPol_3D = new TF3("myPol_3D",&CpPolynom3D::DoPolynom0,-3.1,3.1,-3.1,3.1,0.3,4.9,1);
// myPol_3D = new TF3("myPol_3D",&CpPolynom3D::DoPolynom1,-3.1,3.1,-3.1,3.1,0.3,4.9,4);
// myPol_3D = new TF3("myPol_3D",&CpPolynom3D::DoPolynom2,-3.1,3.1,-3.1,3.1,0.3,4.9,10);
// myPol_3D = new TF3("myPol_3D",&CpPolynom3D::DoPolynom3,-3.1,3.1,-3.1,3.1,0.3,4.9,20);
// myPol_3D = new TF3("myPol_3D",&CpPolynom3D::DoPolynom4,-3.1,3.1,-3.1,3.1,0.3,4.9,35);
// myPol_3D = new TF3("myPol_3D",&CpPolynom3D::DoPolynom5,-3.1,3.1,-3.1,3.1,0.3,4.9,56);
// myPol_3D = new TF3("myPol_3D",&CpPolynom3D::DoPolynom6,-3.1,3.1,-3.1,3.1,0.3,4.9,86);
// myPol_3D = new TF3("myPol_3D",&CpPolynom3D::DoPolynom5,-3.1,3.1,-3.1,3.1,0.3,4.9,56);
// myPol_3D = new TF3("myPol_3D",&CpPolynom3D::DoPolynom6,-3.1,3.1,-3.1,3.1,0.3,4.9,86);
// myPol_3D = new TF3("myPol_3D",&CpPolynom3D::DoPolynom7,-3.1,3.1,-3.1,3.1,0.3,4.9,120);
// myPol_3D = new TF3("myPol_3D",&CpPolynom3D::DoPolynom8,-3.1,3.1,-3.1,3.1,0.3,4.9,165);
// myPol_3D = new TF3("myPol_3D",&CpPolynom3D::DoPolynom9,-3.1,3.1,-3.1,3.1,0.3,4.9,220);
